﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSW.Framework.Common.Binding;
using System.ComponentModel;

namespace CSW.Security.UI.DevEx.Reports
{
    /// <summary>
    /// UserRoleReport
    /// </summary>
    public class UserRoleReport : BindableObject
    {
        /// <summary>Gets the User ID.</summary>
        public string UserID { get; private set; }
        /// <summary>Gets the first name.</summary>
        public string FirstName { get; private set; }
        /// <summary>Gets the last name.</summary>
        public string LastName { get; private set; }
        /// <summary>Gets a value indicating where this user has the App Support flag.</summary>
        public bool IsAppSupport { get; private set; }
        /// <summary>Gets the role description.</summary>
        public string RoleDescription { get; private set; }

        private UserRoleReport(Security_User user, Security_UserRole role)
        {
            UserID = user.UserID;
            FirstName = user.FirstName;
            LastName = user.LastName;
            IsAppSupport = user.IsAppSupport;
            RoleDescription = (role != null ? role.Security_Role.Description : "None");
        }

        /// <summary>
        /// Gets the report.
        /// </summary>
        public static BindingList<UserRoleReport> GetList()
        {
            IEnumerable<UserRoleReport> report =
                from u in SecurityTest.DataContextInstance.Security_User
                join ur in SecurityTest.DataContextInstance.Security_UserRole
                on u equals ur.Security_User into userroles
                from r in userroles.DefaultIfEmpty()
                select new UserRoleReport(u, r);

            return new BindingList<UserRoleReport>(report.ToList());
        }
    }
}
